/*
 *
 *  About palindromic patterns, triangular matrices and A215940.
 *  R. J. Cano Jan 29 2013 (last rev. Feb 19 2013)
 * 
 *  Released under the terms of use for:
 * "The On Line Encyclopedia Of Integer Sequences(R)"
 *
 * 
*/

/*
 * ADDITIONAL INFORMATION FOR A211869:
 * -----------------------------------
 * 
 * You also might be interested on this (full URL: http://oeis.org/w/images/a/a7/VEKTORYNOMIALSdot031.txt)
 * at the end of the present reading.
 *
 * The detection for the lower bound of radices where a(n)= A211869(n) is a palindromic number come as follows:
 * 
 * Let us call r(n) to a function which return the smallest base where a(n) is represented by a palindromic number.
 * 
 * The first two values a(1) and a(2) are respectively 0 and 1. Those values can be used with any radix or base,
 * but for simplicity we might set as their lower bound the common value 3.
 * 
 * For a(3) we have the palindromic number 22 in base 3. Then for offsets smaller than 4, the default value
 * should be r(n)=3, if n<4.
 * 
 * For n equals to or greater than 4, the differences among consecutive values for r(n) are in arithmetic
 * progression by pairs. This might be illustrated with the following...
 * 
 * Example. The first 30 values for r(n) are:
 * 
 * 	r(k) --> 3,3,3,5,7,10,13,17,21,26,31,37,43,50,57,65,73,82,91,101,111,122,133,145,157,170,183,197,211,226 (k in 1..30)
 * 
 * Their first differences are:
 * 
 * r(k+1)-r(k) --> 0,0,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13,14,14,15 (k in 1..29)
 * 
 * Then under the previous observation all what is needed in order to compute the values for r(n) is to implement
 * an iterating function like the one named detectProperBasefromOffset() shown in the present sourcecode.
 * 
 * An informal algorithm description for the iterating routine r(n) is:
 * 
 * Start:  Let be "n" the input.
 * 
 * Step 0  set ans=3, the answer to output.
 * Step 1: if (n < 4) then return ans;
 * Step 2: else set frc=0, and chg=2, respectively the numerator of a fraction counting the pairs, and the difference to sum.
 * Step 3: Iterate for k from 4 to n executing the following:
 * 
 * 	3.1: Increment ans by chg
 * 	3.2: Increment frc
 * 	3.3: If frc is equals to 2 then reset it to zero and increment chg by 1.
 * 
 * Step 4: return ans;
 * 
 * End.
 *
 * Currently at the moment of the present revision it is well known that the previous algortihm is in 
 * fact an alternative description of the sequence A033638: "Quarter squares plus one".
 *
 */

default(echo,0);

radix= 10;
firstCases=13;
padding=3;

A(n)=-1*matrix(n,n,i,j,(i<=j)); /* Named by the author: "The divisorial matrix" operator. Only for the present context. */
                                /* Actually it is usually called the "non-strict upper triangular matrix" */
				
b(n)=vectorv(n,k,k-1);          /* Column vector representation for the smallest posible n-digits permutation.   */

c(n)=vectorv(n,k,n-k);          /* Column vector representation for the greatest posible n-digits permutation.   */

/* Tensor "magic" below: */

Q(n)=A(n)*( c(n) - b(n) );      /* Operating on greatest one of the first n! differences. A term of A215940 */

T(m,k)=if(m>=k,((k-1)*(1+m-k)),0);

/* Print for each k in 1..n the Q(k) column vector which corresponds to
 * the greatest quotient of the kind A215940 among the first k! of them. */

baseconv(n,b=16,s=0)={my(a=[n%b]);while(0<n\=b,a=concat(n%b,a));if(s,s=32*s+23;Strchr(vectorsmall(#a,i,if(a[i]>9,s,48)+a[i])),a)}; /* baseconv() contributed by: M. F. Hasler */

detectProperBasefromOffset(n)={my(frc=0,ans=3,chg=2);if(n>3,for(k=4,n,ans+=chg;if(frc++==2,frc=0;chg++)));ans}

detectProperBasefromVec(v)={my(ans=2);for(j=1,#v,if(v[j]>ans,ans=v[j]));/*if(ans+1<radix,ans=radix,ans++);*/ans++};

demo(n)={my(q,t,H);for(y=1,n,q=Q(y);t=detectProperBasefromOffset(y); print1("Q(",y,")=",q,"; Such column vector represents the number: ",H=sum(s=1,#q,q[s]*t^(#q-s))," (decimal)");if((t<=36),print(" ---> ",baseconv(H,t,1)," (base ",t,")"),print(" ")))};

default(echo,0);

print("\nDemo: On the upper bound for the first m! terms of A215940 (m in 1..13 for example)\n")

demo(firstCases);

print("\nBy inspection of the table, we can deduce the explicit formula: \n\tBound(m,R)=sum_{k=2..m} ((k-1)*(1+m-k))*R^(m-k));\nwhich generates these bounds. Also: \n\tT(m,k)= if(m>=k) then ((k-1)*(1+m-k)) else 0;\nGenerates the same table now in the form of a lower triangular matrix as it is shown below:\n")

/* */
for(a=1,firstCases,for(b=1,firstCases,y=T(a,b); print1(Strchr(vectorsmall(padding-#Str(y)+1,k,32)),y));print(" "));
/* */

print("\nDemo execution finished.\n");

quit();
